//
//  AddSignUsersVC.m
//  HPZZTong
//
//  Created by handpay on 14-1-17.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPAddUserSignViewController.h"
#import "DefineUtil.h"

@interface HPAddUserSignViewController ()

@property(nonatomic, retain)HPDrawAlertView* drawAlertView;
@end

@implementation HPAddUserSignViewController

@synthesize drawAlertView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

//- (BOOL)shouldAutorotate{
//    return NO;
//}
//
//-(NSUInteger)supportedInterfaceOrientations{
//    return UIInterfaceOrientationMaskLandscapeRight;
//}
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
//{
//    return UIInterfaceOrientationLandscapeRight;
//}
- (void)transitionView:(BOOL)isVeri{
    [[UIApplication sharedApplication] setStatusBarHidden:isVeri];
    [[ZZT_UIENG getCurrentNavigation] setNavigationBarHidden:isVeri];
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    [ZZT_UIENG getCurrentNavigation].view.transform = CGAffineTransformIdentity;
    if (isVeri) {
        [ZZT_UIENG getCurrentNavigation].view.transform = CGAffineTransformMakeRotation(M_PI*(90)/180.0);
        [ZZT_UIENG getCurrentNavigation].view.bounds = CGRectMake(0, 0, ScreenHeight, ScreenWidth);
        [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
    }else{
        [ZZT_UIENG getCurrentNavigation].view.transform = CGAffineTransformMakeRotation(M_PI*(0)/180.0);
        [ZZT_UIENG getCurrentNavigation].view.bounds = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait animated:NO];
    }
    
    [UIView commitAnimations];
    [[ZZT_UIENG getCurrentNavigation] setNavigationBarHidden:isVeri];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self transitionView:YES];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self transitionView:NO];
}
- (void)updateUI:(NSNotification*)note{
    NSNumber* numberValue = [note object];
    BOOL isDraw = numberValue.boolValue;
    
    self.okButton.enabled = isDraw;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [NOTIFICATION addObserver:self selector:@selector(updateUI:) name:HPStartDrawNotificationKey object:nil];
    
    [NOTIFICATION addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
    [NOTIFICATION addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    UIView* bgImageView = [self.view viewWithTag:bgImageViewTag];
    if (bgImageView) {
        bgImageView.frame = CGRectMake(0, 0, ScreenHeight, ScreenWidth);
    }
    
    self.mercNameLable.text = ZZT_CLIENG.accountInfo.shopName;

    [self.bgView layer].cornerRadius = 5.0;
    [self.bgView layer].borderColor = kLineFontColor.CGColor;
    [self.bgView layer].borderWidth = 1;
    
    [self.okButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.okButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    
    [self.okButton setBackgroundImage:[[UIImage imageNamed:@"unclickbutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateDisabled];
    
    self.okButton.enabled = NO;
    
    NSUInteger width = self.bgView.frame.size.width;
    LOG(@"width::%d",width);
    if (HP_Device_iPhone5) {
        width += IPHONE5_ADD_HEIGHT;
        
        CGRect checkFrame = self.checkButton.frame;
        CGRect okFrame = self.okButton.frame;
        
        NSUInteger space = (ScreenHeight - checkFrame.size.width - okFrame.size.width) / 3;
        checkFrame.origin.x = space;
        self.checkButton.frame = checkFrame;
        
        space += checkFrame.size.width + space;
        okFrame.origin.x = space;
        
        okFrame.origin.x = space;
        self.okButton.frame = okFrame;
    }
    LOG(@"width222::%d",width);
    HPDrawAlertView* drawView = [[HPDrawAlertView alloc]initWithFrame:CGRectMake(0, 30, width, 185)];
    self.drawAlertView = drawView;
    self.drawAlertView.delegate = self;
    [self.bgView addSubview:self.drawAlertView];
    [drawView release];
    
    self.amountLabel.text = [self.screenData objectForKey:KEY_AMOUNT];
}

- (void)applicationWillResignActive:(NSNotification *)notification
{
    [self transitionView:NO];
}

- (void)applicationDidBecomeActive:(NSNotification *)notification
{
    [self transitionView:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}
- (void)dealloc{
    [NOTIFICATION removeObserver:self];
    self.drawAlertView = nil;
    [_bgView release];
    [_amountLabel release];
    [_mercNameLable release];
    [_checkButton release];
    [_okButton release];
    [super dealloc];
}
#pragma mark -
#pragma mark HPDrawAlertViewDelegate

- (void)drawAlertView:(HPDrawAlertView *)alertView didSaveImage:(UIImage *)image isSign:(BOOL)flag{
//    [HPUIENG popScreen];
//    TransferResultVC* viewController = (TransferResultVC*)[HPUIENG getCurrentScreen];
//    
//    UIButton* button = (UIButton*)[viewController.view viewWithTag:TAG_SIGN_BUTTON];
//    viewController.signImageView.image = image;
//   
//    viewController.isSign = flag;
//    
//    if (flag) {
//        viewController.signImageView.hidden = NO;
//         [button setTitle:@"" forState:UIControlStateNormal];
//    }else{
//        viewController.signImageView.hidden = YES;
//        [button setTitle:@"请输入签名" forState:UIControlStateNormal];
//    }
}

- (void)viewDidUnload {
    [self setBgView:nil];
    [self setAmountLabel:nil];
    [self setMercNameLable:nil];
    [self setCheckButton:nil];
    [self setOkButton:nil];
    [super viewDidUnload];
}
- (IBAction)checkButtonClick:(id)sender {
    UIButton* button = (UIButton*)sender;
    button.selected = !button.selected;
}

- (IBAction)okButtonClick:(id)sender {
    UIImage* signImage = [self.drawAlertView getImage];
    
    if (self.checkButton.selected) {
        NSMutableDictionary* muDic = [NSMutableDictionary dictionaryWithDictionary:self.screenData];
        [muDic setObject:signImage forKey:@"signImage"];
        [HPUIENG showScreen:SCR_SEND_SIGN params:muDic];
    }else{
//        [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait animated:NO];
        
        CallbackData* callback_ = [self.screenData objectForKey:KEY_CALLBACK];
        LOG(@"addusersign callback:%@",callback_.target);
        if (callback_ && [callback_.target respondsToSelector:callback_.callback]) {
            [callback_.target performSelector:callback_.callback withObject:signImage withObject:nil];
        }

        [HPUIENG popScreen];
    }
    
}
@end
